package com.sftz.modules.minapp.resourceRecord.service;

import java.io.IOException;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.SqlManager;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.sftz.assets.entities.CourseEntity;
import com.sftz.assets.entities.grade.GradeCourseRelationEntity;
import com.sftz.assets.enums.AnswerStatusEnum;
import com.sftz.assets.enums.CourseGradeTypeEnum;
import com.sftz.modules.minapp.resourceRecord.dto.PersonActivitiesListDto;
import com.sftz.modules.minapp.resourceRecord.dto.PersonHomeWorkListDto;
import com.sftz.modules.minapp.resourceRecord.dto.PersonPaperListDto;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.dao.IDbDao;
import com.uxuexi.core.db.util.DbSqlUtil;

/**
 * @author   DuKL  各项得分
 * @Date	 2018年12月21日
 */
@IocBean
public class StudentStudyCourseViewService {

	@Inject
	private StudentOtherCourseScoreViewService studentOtherCourseScoreViewService;

	@Inject
	private CourseResourceStudyViewService courseResourceStudyViewService;

	@Inject
	private IDbDao dbDao;

	@Inject
	private SqlManager sqlManager;

	/**
	 * @author   DuKL
	 * @throws IOException 
	 * @Date   2018年12月21日
	 * (effect:音视频学习得分)
	 */
	@Aop("logTime")
	public double calculateVAScore(Integer courseId, Integer userId) {
		double vaScore = 0;
		//当前课程下的音视频时长总和
		double findCourseResTime = courseResourceStudyViewService.findCourseResTime(courseId);
		if (findCourseResTime != 0) {
			//当前课程下的音频和视频学习合格应得分数
			GradeCourseRelationEntity gradeCR = dbDao.fetch(GradeCourseRelationEntity.class,
					Cnd.where("courseId", "=", courseId)
							.and("scoresOfId", "=", CourseGradeTypeEnum.STUDYGRADE.intKey()));
			if (!Util.isEmpty(gradeCR)) {
				Double gradeStudy = gradeCR.getScore();
				if (!Util.isEmpty(gradeStudy)) {
					//查询课程学习合格百分比
					CourseEntity course = dbDao.fetch(CourseEntity.class, Cnd.where("id", "=", courseId));
					Double passHourRate = course.getPassHourRate();
					if (!Util.isEmpty(passHourRate)) {
						double studyCourse = studentOtherCourseScoreViewService.studyCourseAndResTime(courseId, userId,
								findCourseResTime);
						if (studyCourse >= passHourRate) {
							return gradeStudy;
						}
					}
				}
			}

		}
		return vaScore;
	}

	/**
	 * @author   DuKL
	 * @Date	 2018年12月21日
	 * (effect:作业得分)
	 */
	public double calculateHwScore(Integer courseId, Integer userId) {
		double hwScore = 0;
		Sql studentHwScore = Sqls.create(sqlManager
				.get("minapp_resourceRecord_teaching_center_personStudy_homework_report_list"));
		studentHwScore.setParam("courseId", courseId);
		studentHwScore.setParam("userId", userId);
		studentHwScore.setParam("status", AnswerStatusEnum.start.intKey());
		List<PersonHomeWorkListDto> homeworkList = DbSqlUtil.query(dbDao, PersonHomeWorkListDto.class, studentHwScore);
		for (PersonHomeWorkListDto personHome : homeworkList) {
			Double grade = personHome.getGrade();
			Integer passScore = personHome.getPassScore();
			if (!Util.isEmpty(grade) && !Util.isEmpty(passScore)) {
				if (grade >= passScore) {
					double hwAlreadyScore = personHome.getHwAlreadyScore();
					if (!Util.isEmpty(hwAlreadyScore)) {
						hwScore += personHome.getHwAlreadyScore();
					}
				}
			}
		}
		return hwScore;
	}

	/**
	 * @author   DuKL
	 * @Date	 2018年12月21日
	 * (effect:考试得分)
	 */
	public double calculatePaperScore(Integer courseId, Integer userId) {
		double paperScore = 0;
		Sql studentPaperScore = Sqls.create(sqlManager
				.get("minapp_resourceRecord_teaching_center_personStudy_paper_report_list"));
		studentPaperScore.setParam("courseId", courseId);
		studentPaperScore.setParam("userId", userId);
		studentPaperScore.setParam("status", AnswerStatusEnum.start.intKey());
		List<PersonPaperListDto> paperList = DbSqlUtil.query(dbDao, PersonPaperListDto.class, studentPaperScore);
		for (PersonPaperListDto personPaper : paperList) {
			double grade = personPaper.getGrade();
			Integer passScore = personPaper.getPassScore();
			if (!Util.isEmpty(grade) && !Util.isEmpty(passScore)) {
				if (grade >= passScore) {
					double paperAlreadyScore = personPaper.getPaperAlreadyScore();
					if (!Util.isEmpty(paperAlreadyScore)) {
						paperScore += paperAlreadyScore;
					}
				}
			}
		}
		return paperScore;
	}

	/**
	 * @author   DuKL
	 * @Date	 2018年9月27日
	 * (effect:活动得分)
	 */
	public double calculateActivitScore(Integer courseId, Integer userId) {
		Sql activit = Sqls.create(sqlManager
				.get("minapp_resourceRecord_teaching_center_personStudy_activity_report_list"));
		activit.setParam("courseId", courseId);
		activit.setParam("userId", userId);
		List<PersonActivitiesListDto> activitList = DbSqlUtil.query(dbDao, PersonActivitiesListDto.class, activit);
		double activitScore = 0;
		for (PersonActivitiesListDto personActivities : activitList) {
			double actualScore = personActivities.getActualScore();
			if (!Util.isEmpty(actualScore)) {
				activitScore += actualScore;
			}
		}
		return activitScore;
	}

}
